/**
 * 版权所有：厦门市巨龙信息科技有限公司
 * Copyright 2023 Xiamen Dragoninfo Eng. Co. Ltd.
 * All right reserved.
 */
package com.github.alfred.database.adapter.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import com.github.alfred.database.DatabaseAdapterHandler;
import com.github.alfred.database.adapter.IDatabaseAdapterProvider;
import com.github.alfred.exception.Assert;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 类说明
 *
 * @author chengh
 * @date 2023/9/15 13:49
 */
@Component
public class DatabaseAdapter {
    @Autowired
    private MybatisPlusProperties mybatisPlusProperties;

    @Autowired
    private DataSourceProperties properties;

    public String getSortNullsLast(String column, Sort.Direction direction) {
        if (StringUtils.isBlank(column)) {
            return "";
        }
        String databaseId = getDatabaseId();
        IDatabaseAdapterProvider databaseAdapterProvider = DatabaseAdapterHandler.getHandler(databaseId);
        Assert.isTrue(Objects.nonNull(databaseAdapterProvider), format(databaseId));
        return databaseAdapterProvider.getSortNullsLast(column, direction);
    }

    /**
     * 空排序靠前
     *
     * @param column
     * @return
     */
    String getSortNullsFirst(String column, Sort.Direction direction) {
        if (StringUtils.isBlank(column)) {
            return "";
        }
        String databaseId = getDatabaseId();
        IDatabaseAdapterProvider databaseAdapterProvider = DatabaseAdapterHandler.getHandler(databaseId);
        Assert.isTrue(Objects.nonNull(databaseAdapterProvider), format(databaseId));
        return databaseAdapterProvider.getSortNullsFirst(column, direction);
    }

    private String getDatabaseId() {
        String databaseId = mybatisPlusProperties.getConfiguration().getDatabaseId();
        if (StringUtils.isBlank(databaseId)) {
            DbType dbType = JdbcUtils.getDbType(properties.getUrl());
            databaseId = dbType.getDb();
        }
        return databaseId;
    }

    /**
     * 字符转时间戳类型
     *
     * @param time 时间
     * @return String
     */
    public String strToTimestamp(String time) {
        if (StringUtils.isBlank(time)) {
            return "";
        }
        String databaseId = getDatabaseId();
        IDatabaseAdapterProvider databaseAdapterProvider = DatabaseAdapterHandler.getHandler(databaseId);
        Assert.isTrue(Objects.nonNull(databaseAdapterProvider), format(databaseId));
        return databaseAdapterProvider.strToTimestamp(time);
    }

    private String format(String code) {
        return String.format("%s %s", code, "未适配");
    }
}
